package com.yhsoft.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yhsoft.common.service.Po.TokenUser;
import com.yhsoft.common.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
/**
 * @author: yyyyyyyh
 * @create: 2023-09-23
 * @Version 1.0
 **/
@Slf4j
@Component
public class MybatisPlusConfig implements MetaObjectHandler{
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill...");
        TokenUser user = TokenService.getUserBytoken();
        this.setFieldValByName("deleted", 0, metaObject);
        this.setFieldValByName("createdBy", user.getId(), metaObject);
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updatedBy", user.getId(), metaObject);
        this.setFieldValByName("updatedTime", LocalDateTime.now(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill...");
        TokenUser user = TokenService.getUserBytoken();
        this.setFieldValByName("updatedBy", user.getId(), metaObject);
        this.setFieldValByName("updatedTime", LocalDateTime.now(), metaObject);
    }
}


